package com.hctforgreen.greenservice;

import java.util.List;

import org.taptwo.android.widget.ViewFlow;
import org.taptwo.android.widget.ViewFlow.ViewSwitchListener;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.ParentActivity;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.ExamSubEntity;
import com.hctforgreen.greenservice.service.HctApplication;
import com.hctforgreen.greenservice.ui.adapter.TerminalExamDetailChildListAdapter;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.KeyboardUtil;

/**
 * 终端培训：错题详情页
 * 答案解析
 * 2016-9-22
 * @author 390694
 * 
 */
public class TerminalWroExamDetailActivity extends ParentActivity {
	private ViewFlow mViewFlow = null;
	private TerminalExamDetailChildListAdapter mAdapter = null;
	// private ExamPaperEntity mPaperEntity;
	private int position;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.count_down_exam_detail_activity);
		initSkinLayout();
		initFinshExamineActivityReceiver();
		Intent intent = getIntent();
		// mPaperEntity = (ExamPaperEntity) intent
		// .getSerializableExtra(HctConstants.ON_ACTIVITY_KEY_EXAM_ENTITY);
		position = intent.getIntExtra("position", 0);
		initTitleButtonBar();
		initWindow();
	}

	private void initTitleButtonBar() {
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.exam_answer_count));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		// Button backBtn = (Button) findViewById(R.id.left_btn);
		// backBtn.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// finish();
		// }
		// });
		//
	}

	private void initWindow() {
		List<ExamSubEntity> dataLst = HctApplication.terminalExamSubEntities;
		if (dataLst == null) {
			return;

		}
		HctApplication.terminalExamSubEntities = null;
		initViewFlow(dataLst);
		initBottomBarBtns();
	}

	private void initBottomBarBtns() {
		/**
		 * 上一题
		 */
		final View prevBtn = findViewById(R.id.lyt_prev);
		/**
		 * 下一题
		 */
		final View nextBtn = findViewById(R.id.lyt_next);

		// if (mAdapter.getmIndex() == 0) {
		// prevBtn.setVisibility(View.GONE);
		// }
		// if (mAdapter.getmIndex() == mAdapter.getCount() - 1) {
		// nextBtn.setVisibility(View.GONE);
		// }

		prevBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mAdapter.getmIndex() != 0) {
					mViewFlow.setSelection(mAdapter.getmIndex() - 1);
				} else {
					Toast.makeText(TerminalWroExamDetailActivity.this,
							getString(R.string.is_always_first_item),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		nextBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mAdapter.getmIndex() < mAdapter.getCount() - 1) {
					mViewFlow.setSelection(mAdapter.getmIndex() + 1);
				} else {
					Toast.makeText(TerminalWroExamDetailActivity.this,
							getString(R.string.is_always_last_item),
							Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	private void initViewFlow(final List<ExamSubEntity> childEntities) {
		if (mViewFlow == null) {
			mViewFlow = (ViewFlow) findViewById(R.id.viewflow);
			mViewFlow.setEnableScroll(false);
			mViewFlow.setbResetFoucusInSelection(true);

			// List<ExamEntity> childEntities = new ArrayList<ExamEntity>();
			// childEntities.add(examEntity);

			mAdapter = new TerminalExamDetailChildListAdapter(
					TerminalWroExamDetailActivity.this, mViewFlow, childEntities,
					HctConstants.ON_ACTIVITY_FROM_WRO_EXAMS_DETAIL_ACTIVITY);

			mViewFlow.setAdapter(mAdapter);
			mViewFlow.setOnViewSwitchListener(new ViewSwitchListener() {

				@Override
				public void onSwitched(View view, int position) {

					mAdapter.setmIndex(position);
					ExamSubEntity entity = (ExamSubEntity) mAdapter
							.getItem(position);
					changeTitle(entity);
					findViewById(R.id.lyt_prev).setVisibility(View.VISIBLE);
					findViewById(R.id.lyt_next).setVisibility(View.VISIBLE);
					if (position == 0) {
						findViewById(R.id.lyt_prev).setVisibility(
								View.INVISIBLE);
					} else if (position == mAdapter.getCount() - 1) {
						findViewById(R.id.lyt_next).setVisibility(
								View.INVISIBLE);
					}
				}

				/**
				 * 标题
				 * 
				 * @param entity
				 */
				private void changeTitle(ExamSubEntity entity) {
					TextView indicTv = (TextView) findViewById(R.id.tv_title);
					if (entity.type.equals("1")) {
						KeyboardUtil
								.hideSoftKeyboard(TerminalWroExamDetailActivity.this);
						indicTv.setText(getString(R.string.q_type_1_1));
					} else if (entity.type.equals("2")) {
						KeyboardUtil
								.hideSoftKeyboard(TerminalWroExamDetailActivity.this);
						indicTv.setText(getString(R.string.q_type_1_2));
					} else if (entity.type.equals("3")) {
						KeyboardUtil
								.hideSoftKeyboard(TerminalWroExamDetailActivity.this);
						indicTv.setText(getString(R.string.q_type_2));
					} else if (entity.type.equals("4")) {
						indicTv.setText(getString(R.string.q_type_3));
					}
				}
			});
			mViewFlow.setSelection(position);
			KeyboardUtil.hideSoftKeyboard(TerminalWroExamDetailActivity.this);
		}

	}

}
